package com.testing.redboy;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.igexin.sdk.PushManager;
import com.testing.redboy.fragment.ClassFragment;
import com.testing.redboy.fragment.HomeFragment;
import com.testing.redboy.fragment.Info_Fragment;
import com.testing.redboy.fragment.MoreFragment;
import com.testing.redboy.fragment.QQFragment;
import com.testing.redboy.fragment.SearchFragment;
import com.testing.redboy.fragment.ShoppingFragment;
import com.testing.redboy.fragment.Time_Fragment;
import com.testing.redboy.fragment.Tui_Fragment;
import com.testing.redboy.fragment.ZxingFragment;

public class FirstActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView home;
    private ImageView search;
    private ImageView classes;
    private ImageView shopping;
    private ImageView more;
    private int[] xuan = new int[]{R.drawable.bar_home_selected, R.drawable.bar_search_selected, R.drawable.bar_class_selected, R.drawable.bar_shopping_selected, R.drawable.bar_more_selected};
    private int[] buxuan = new int[]{R.drawable.bar_home_normal, R.drawable.bar_search_normal, R.drawable.bar_class_normal, R.drawable.bar_shopping_normal, R.drawable.bar_more_normal};
    private ImageView[] list;

    public HomeFragment homef;
    SearchFragment searchf;
    ClassFragment classf;
    ShoppingFragment shopf;
    MoreFragment moref;
    Time_Fragment timef;
    Tui_Fragment tuif;
    Info_Fragment infof;
    ZxingFragment zf;
    QQFragment qqf;

    Fragment old_f;

    String[] url = new String[]{
            "http://120.27.126.103:8080/ECServer_D/limitbuy",
            "http://120.27.126.103:8080/ECServer_D/brand",
            "http://120.27.126.103:8080/ECServer_D/product"
    };

    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        //个推
        // com.getui.demo.DemoPushService 为第三方自定义推送服务
        PushManager.getInstance().initialize(this.getApplicationContext(), DemoPushService.class);
        // com.getui.demo.DemoIntentService 为第三方自定义的推送服务事件接收类
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();


        initView();

        setLis();

        list = new ImageView[]{home, search, classes, shopping, more};

        if (homef == null) {
            homef = new HomeFragment();
        }
        addFragment(homef);

        Intent intent = getIntent();
        int fk = intent.getIntExtra("fk", 0);
        switch (fk) {
            case 5:
                if (timef == null) {
                    timef = Time_Fragment.newInstance("", url[0]);
                }
                addFragment(timef);
                break;
            case 6:
                if (tuif == null) {
                    tuif = Tui_Fragment.newInstance("", url[1]);
                }
                addFragment(tuif);
                break;
            case 7:
                if (infof == null) {
                    infof = Info_Fragment.newInstance("", url[2]);
                }
                addFragment(infof);
                break;
            case 8:
                if (searchf == null) {
                    searchf = new SearchFragment();
                }
                addFragment(searchf);
                break;
            case 9:
                if (zf == null) {
                    zf = new ZxingFragment();
                }
                addFragment(zf);
                break;
            case 10:
                if (qqf == null) {
                    qqf = new QQFragment();
                }
                addFragment(qqf);
                break;
        }
    }

    private void setLis() {
        home.setOnClickListener(this);
        search.setOnClickListener(this);
        classes.setOnClickListener(this);
        shopping.setOnClickListener(this);
        more.setOnClickListener(this);
    }

    private void initView() {
        home = (ImageView) findViewById(R.id.home);
        search = (ImageView) findViewById(R.id.search);
        classes = (ImageView) findViewById(R.id.classes);
        shopping = (ImageView) findViewById(R.id.shopping);
        more = (ImageView) findViewById(R.id.more);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home:
                changeIcon(0);
                if (homef == null) {
                    homef = new HomeFragment();
                }
                addFragment(homef);
                break;
            case R.id.search:
                changeIcon(1);
                if (searchf == null) {
                    searchf = new SearchFragment();
                }
                addFragment(searchf);
                break;
            case R.id.classes:
                changeIcon(2);
                if (classf == null) {
                    classf = new ClassFragment();
                }
                addFragment(classf);
                break;
            case R.id.shopping:
                changeIcon(3);
                if (shopf == null) {
                    shopf = new ShoppingFragment();
                }
                addFragment(shopf);
                break;
            case R.id.more:
                changeIcon(4);
                if (moref == null) {
                    moref = new MoreFragment();
                }
                addFragment(moref);
                break;
        }
    }

    public void changeIcon(int index) {
        for (int i = 0; i < xuan.length; i++) {
            if (i == index) {
                list[i].setBackgroundResource(xuan[i]);
            } else {
                list[i].setBackgroundResource(buxuan[i]);
            }
        }
    }

    public void addFragment(Fragment new_f) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (old_f != null) {
            ft.hide(old_f);
        }
        if (!new_f.isAdded()) {
            ft.add(R.id.empty, new_f);
        }

        ft.show(new_f);
        ft.commit();

        old_f = new_f;
    }
}
